package com.imooc.ecommerce.service;

import brave.Tracer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * @author Yaxi.Zhang
 * @since 2022/11/10 01:17
 */
@Slf4j
@Service
public class SleuthTraceInfoService {
    /**
     * brave.Tracer 跟踪对象
     */
    private final Tracer tracer;

    public SleuthTraceInfoService(Tracer tracer) {
        this.tracer = tracer;
    }

    /**
     * <p>打印当前的跟踪信息到日志中
     */
    public void logCurrentTraceInfo() {
        log.info("Sleuth trace id: [{}]", Long.toHexString(tracer.currentSpan().context().traceId()));
        log.info("Sleuth span id: [{}]", Long.toHexString(tracer.currentSpan().context().spanId()));
    }

}
